package controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.BookBorrow;
import pojo.BookMessage;
import pojo.UserMessage;
import service.AdminBookService;
import service.UserBookService;

import javax.servlet.http.HttpServletRequest;
import java.awt.print.Book;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
public class userAction {
    @Autowired
    AdminBookService adminBookService;
    @Autowired
    UserBookService userBookService;

    /**
     * 去到用户功能页面
     */
    @RequestMapping("/touserAction")
    public String touserAction() {
        return "userAction";
    }

    /**
     * 用户查看所有书籍
     */
    @RequestMapping("/toBooksList")
    public String toBooksList(Model model, @RequestParam(name = "page", required = false, defaultValue = "1") int page,
                              @RequestParam(name = "size", required = false, defaultValue = "5") int size) {
        PageHelper.startPage(page, size);
        List<BookMessage> books = userBookService.getAllBooks();
        PageInfo<BookMessage> pageInfo = new PageInfo<>(books);
        List<BookMessage> list = pageInfo.getList();
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("books", list);
        return "BooksList";
    }

    /**
     * 用户搜索某一图书
     */
    @RequestMapping("/searchBook")
    public String serachBook(Model model, String bookName, @RequestParam(name = "page", required = false, defaultValue = "1") int page, @RequestParam(name = "size", required = false, defaultValue = "5") int size) {
        PageHelper.startPage(page, size);
        HashMap<String, Object> map = new HashMap<>();
        map.put("bookInfo", bookName.trim());
        List<BookMessage> book = userBookService.getBooksByInfo(map);
        PageInfo<BookMessage> pageInfo = new PageInfo<>(book);
        List<BookMessage> books = pageInfo.getList();
        model.addAttribute("books", books);
        model.addAttribute("bookName", bookName);
        model.addAttribute("pageInfo", pageInfo);
        System.out.println(pageInfo.getPages());
        return "UserSearchBookList";
    }

    /**
     * 用户借阅某一图书
     */
    @RequestMapping("/jieyue/{bookName}/{bookIsbn}/{bookInfo}/{bookAuthor}/{bookDate}/{bookConcern}")
    public String order(@PathVariable String bookName, @PathVariable String bookIsbn, @PathVariable String bookInfo,
                        @PathVariable String bookAuthor, @PathVariable String bookDate, @PathVariable String bookConcern, HttpServletRequest req) {
        System.out.println("AAAA123456789");
        System.out.println(bookName);
        String userName = (String) req.getSession().getAttribute("userName");
        System.out.println(userName);
        long time = System.currentTimeMillis();
        Date date = new Date(time);
        SimpleDateFormat ab = new SimpleDateFormat("yyyy-MM-dd");
        BookBorrow ao = new BookBorrow(userName, bookName, bookIsbn, bookInfo, ab.format(date), bookAuthor, bookConcern, bookDate);
        userBookService.order(ao);
        adminBookService.deleteBook(bookName);
        return "redirect:/toBooksList";
    }

    /**
     * 获取用户信息
     */
    @RequestMapping("/tousermessage")
    public String tousermessage(Model model, HttpServletRequest req) {
        String userName = (String) req.getSession().getAttribute("userName");
        UserMessage a = userBookService.getUserMessage(userName);
        ArrayList<UserMessage> user = new ArrayList<>();
        if (a != null) {
            user.add(a);
        }
        model.addAttribute("user", user);
        return "userMessage";
    }

    /**
     * 用户获取个人借阅书籍
     */
    @RequestMapping("/toBooks")
    public String tojiyue(Model model, HttpServletRequest req) {
        String userName = (String) req.getSession().getAttribute("userName");
        System.out.println(userName);
        List<BookBorrow> books = userBookService.getBorrow(userName);
        System.out.println(books.size());
        model.addAttribute("books", books);
        return "lendList";
    }

    @RequestMapping("/tuihuan/{bookName}/{bookIsbn}/{bookInfo}/{bookAuthor}/{bookDate}/{bookConcern}")
    public String tojie(@PathVariable String bookName, @PathVariable String bookIsbn, @PathVariable String bookInfo,
                        @PathVariable String bookAuthor, @PathVariable String bookDate, @PathVariable String bookConcern, Model model, HttpServletRequest req) {
        String userName = (String) req.getSession().getAttribute("userName");
        System.out.println(userName);
        System.out.println("123321");
        int d = adminBookService.delete(bookName, userName);
        BookMessage book = new BookMessage(bookName, bookAuthor, bookConcern, bookIsbn, bookInfo, bookDate);
        adminBookService.addBook(book);
        System.out.println(d);
        List<BookBorrow> books = userBookService.getBorrow(userName);
        model.addAttribute("books", books);
        return "lendList";
    }

    /**
     * 用户获取个人信息
     */
    @RequestMapping("/getuser")
    public String getuser(Model model, HttpServletRequest req) {
        String userName = (String) req.getSession().getAttribute("userName");
        UserMessage user = userBookService.getUserMessage(userName);
        model.addAttribute("user", user);
        return "UpdateUserMessage";
    }

    /**
     * 用户修改个人信息
     */
    @RequestMapping("/update")
    @ResponseBody
    public String update(UserMessage user) throws IOException {
        String str = "";
        int d = userBookService.UpdateuserMessage(user);
        if (d > 0) {
            str += "修改成功!";
        }
        return str;
    }

    /**
     * 用户删除个人账号
     */
    @RequestMapping("/deleteUser")
    @ResponseBody
    public String deleteUser(HttpServletRequest req) {
        int i = 0;
        String str = "";
        String name = (String) req.getSession().getAttribute("userName");
        int list = adminBookService.getLends(name);
        if (list == 0) {
            i = userBookService.deleteUserByname(name);
        }
        str += i;
        return str;
    }
}

